/*
 * Copyright (C), 2002-2017, 南京苏宁软件技术有限公司
 * FileName: AreaTaskDaoImpl.java
 * Author:   10070706
 * Date:     2017年1月10日 下午2:50:52
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.service.impl.dao.mainpushnew;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.constants.MainPushConstants;
import com.suning.sawp.intf.dao.mainpushnew.AreaTaskDao;
import com.suning.sawp.po.mainpushnew.AreaModelRel;
import com.suning.sawp.po.mainpushnew.AreaTask;
import com.suning.sawp.po.mainpushnew.StoreTask;
import com.suning.sawp.vo.mainpushnew.AreaTaskDetailVo;
import com.suning.sawp.vo.mainpushnew.AreaTaskModelCompleteVo;
import com.suning.sawp.vo.mainpushnew.AreaTaskModelDetailVo;
import com.suning.sawp.vo.mainpushnew.AreaTaskStoreDetailVo;
import com.suning.sawp.vo.mainpushnew.AreaTaskTargetExportVo;
import com.suning.sawp.vo.mainpushnew.AreaTaskVo;
import com.suning.sawp.vo.mainpushnew.CalStoreTaskDistributeVo;
import com.suning.sawp.vo.mainpushnew.StoreInfoVo;
import com.suning.sawp.vo.mainpushnew.StoreTaskTotalAmountVo;
import com.suning.sawp.vo.mainpushnew.StoreTaskTypeActualVo;
import com.suning.sawp.vo.mainpushnew.StoreTaskTypeTargetVo;

/**
 * 功能简述: 大区任务Dao实现<br>
 *
 * @author 10070706 陈辉
 */
@Repository
public class AreaTaskDaoImpl implements AreaTaskDao {

    @Autowired
    private DalClient dalClient;

    @Override
    public String query5DisCodeByUserId(String userId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", userId);
        return dalClient.queryForObject("areaTask.query5DisCodeByUserId", param, String.class);
    }

    @Override
    public List<AreaTaskVo> queryAreaTaskInfo(String disCode, String taskTopic, String month, Integer startRow,
            Integer pageLimit) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("disCode", disCode);
        param.put("taskTopic", taskTopic);
        param.put("month", month);
        param.put("startRow", startRow);
        param.put("pageLimit", pageLimit);
        return dalClient.queryForList("areaTask.queryAreaTaskInfo", param, AreaTaskVo.class);
    }

    @Override
    public Integer queryAreaTaskInfoSize(String disCode, String taskTopic, String month, Integer startRow,
            Integer pageLimit) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("disCode", disCode);
        param.put("taskTopic", taskTopic);
        param.put("month", month);
        param.put("startRow", startRow);
        param.put("pageLimit", pageLimit);
        return dalClient.queryForObject("areaTask.queryAreaTaskInfoSize", param, Integer.class);
    }

    @Override
    public List<StoreTaskTypeTargetVo> queryStoreTaskTargetInfo(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryStoreTaskTargetInfo", param, StoreTaskTypeTargetVo.class);
    }

    @Override
    public List<StoreTaskTypeActualVo> queryStoreTaskActualSaleInfo(Long hqTaskId, String disCode) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("hqTaskId", hqTaskId);
        param.put("disCode", disCode);
        return dalClient.queryForList("areaTask.queryStoreTaskActualSaleInfo", param, StoreTaskTypeActualVo.class);
    }

    @Override
    public List<StoreTaskTotalAmountVo> queryTotalAmountOfCate(String disCode, String cateCode, String month) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("disCode", disCode);
        param.put("cateCode", cateCode);
        param.put("month", month);
        return dalClient.queryForList("areaTask.queryTotalAmountOfCate", param, StoreTaskTotalAmountVo.class);
    }

    @Override
    public AreaTaskDetailVo queryAreaTaskDetailByAreaTaskId(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForObject("areaTask.queryAreaTaskDetailByAreaTaskId", param, AreaTaskDetailVo.class);
    }

    @Override
    public Integer queryAreaTaskTotalTargetNum(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForObject("areaTask.queryAreaTaskTotalTargetNum", param, Integer.class);
    }

    @Override
    public Integer queryAreaTaskTotalCompleteNum(Long hqTaskId, String disCode) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("hqTaskId", hqTaskId);
        param.put("disCode", disCode);
        return dalClient.queryForObject("areaTask.queryAreaTaskTotalCompleteNum", param, Integer.class);
    }

    @Override
    public AreaTask selectAreaTaskByIdAndDisCode(Long hqTaskId, String disCode) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("hqTaskId", hqTaskId);
        param.put("disCode", disCode);
        return dalClient.queryForObject("areaTask.selectAreaTaskByIdAndDisCode", param, AreaTask.class);
    }

    @Override
    public List<AreaTaskModelDetailVo> queryAreaTaskModelDetailList(Long areaTaskId, Integer startRow,
            Integer pageLimit) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        param.put("startRow", startRow);
        param.put("pageLimit", pageLimit);
        return dalClient.queryForList("areaTask.queryAreaTaskModelDetailList", param, AreaTaskModelDetailVo.class);
    }

    @Override
    public Integer queryAreaTaskModelDetailSize(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForObject("areaTask.queryAreaTaskModelDetailSize", param, Integer.class);
    }

    @Override
    public List<AreaTaskModelCompleteVo> queryAreaTaskModelComplete(String disCode, String cateCode, String month) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("disCode", disCode);
        param.put("cateCode", cateCode);
        param.put("month", month);
        return dalClient.queryForList("areaTask.queryAreaTaskModelComplete", param, AreaTaskModelCompleteVo.class);
    }

    @Override
    public List<AreaTaskStoreDetailVo> queryStoreTaskDistributeDetailByAreaTaskIdAndModelIdForAllStore(Long areaTaskId,
            Long modelId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        param.put("modelId", modelId);
        return dalClient.queryForList("areaTask.queryStoreTaskDistributeDetailByAreaTaskIdAndModelIdForAllStore", param,
                AreaTaskStoreDetailVo.class);
    }

    @Override
    public List<AreaTaskStoreDetailVo> queryStoreTaskDistributeDetailByAreaTaskIdAndModelId(Long areaTaskId,
            Long modelId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        param.put("modelId", modelId);
        return dalClient.queryForList("areaTask.queryStoreTaskDistributeDetailByAreaTaskIdAndModelId", param,
                AreaTaskStoreDetailVo.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void updateStoreTaskModelNum(List<AreaTaskStoreDetailVo> update) {
        Map<String, Object>[] params = new Map[update.size()];
        Map<String, Object> param = null;
        AreaTaskStoreDetailVo store = null;
        for (int i = 0; i < update.size(); i++) {
            store = update.get(i);
            param = new HashMap<String, Object>();
            param.put("storeTaskId", store.getStoreTaskId());
            param.put("modelId", store.getModelId());
            param.put("num", store.getTargetNum());
            param.put("distStatus", MainPushConstants.STORE_TASK_MODEL_STATUS_REDISTRIBUTE);
            params[i] = param;
        }
        dalClient.batchUpdate("areaTask.updateStoreTaskModelNum", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void batchSaveTaskModelNum(Long hqTaskId, List<AreaTaskStoreDetailVo> insert) {
        Map<String, Object>[] params = new Map[insert.size()];
        Map<String, Object> param = null;
        AreaTaskStoreDetailVo store = null;
        for (int i = 0; i < insert.size(); i++) {
            store = insert.get(i);
            param = new HashMap<String, Object>();
            param.put("storeTaskId", store.getStoreTaskId());
            param.put("hqTaskId", hqTaskId);
            param.put("modelId", store.getModelId());
            param.put("targetNum", store.getTargetNum());
            param.put("distStatus", MainPushConstants.STORE_TASK_MODEL_STATUS_UNDISTRIBUTED);
            params[i] = param;
        }
        dalClient.batchUpdate("areaTask.batchSaveTaskModelNum", params);
    }

    @Override
    public List<AreaTaskTargetExportVo> queryAreaTaskModelDetail4Export(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryAreaTaskModelDetail4Export", param, AreaTaskTargetExportVo.class);
    }

    @Override
    public List<StoreInfoVo> queryStoreInfo(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryStoreInfo", param, StoreInfoVo.class);
    }

    @Override
    public List<StoreTask> queryStoreTaskListByAreaTaskId(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryStoreTaskListByAreaTaskId", param, StoreTask.class);
    }

    @Override
    public List<AreaTask> queryAreaTaskByHqTaskId(Long hqTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("hqTaskId", hqTaskId);
        return dalClient.queryForList("areaTask.queryAreaTaskByHqTaskId", param, AreaTask.class);
    }

    @Override
    public List<AreaModelRel> queryAreaModelRel(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryAreaModelRel", param, AreaModelRel.class);
    }

    @Override
    public AreaModelRel queryAreaModelRelById(Long areaTaskId, Long modelId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        param.put("modelId", modelId);
        return dalClient.queryForObject("areaTask.queryAreaModelRelById", param, AreaModelRel.class);
    }

    @Override
    public AreaTask queryAreaTaskByAreaTaskId(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForObject("areaTask.queryAreaTaskByAreaTaskId", param, AreaTask.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void delNotNeedStoreModel(List<AreaTaskStoreDetailVo> need2Del) {
        Map<String, Object>[] params = new Map[need2Del.size()];
        Map<String, Object> param = null;
        AreaTaskStoreDetailVo store = null;
        for (int i = 0; i < need2Del.size(); i++) {
            store = need2Del.get(i);
            param = new HashMap<String, Object>();
            param.put("storeTaskId", store.getStoreTaskId());
            param.put("modelId", store.getModelId());
            params[i] = param;
        }
        dalClient.batchUpdate("areaTask.delNotNeedStoreModel", params);
    }

    @Override
    public void modifyAreaModelRelStatus(Long areaTaskId, Long modelId, Short status) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        param.put("modelId", modelId);
        param.put("status", status);
        dalClient.execute("areaTask.modifyAreaModelRelStatus", param);
    }

    @Override
    public void calStoreTaskDistributeNum(List<CalStoreTaskDistributeVo> calNum) {
        @SuppressWarnings("unchecked")
        Map<String, Object>[] params = new Map[calNum.size()];
        Map<String, Object> param = null;
        CalStoreTaskDistributeVo dist = null;
        for (int i = 0; i < calNum.size(); i++) {
            dist = calNum.get(i);
            param = new HashMap<String, Object>();
            param.put("storeTaskId", dist.getStoreTaskId());
            param.put("targetNum", dist.getTargetNum());
            params[i] = param;
        }
        dalClient.batchUpdate("areaTask.calStoreTaskDistributeNum", params);

    }

    @Override
    public List<CalStoreTaskDistributeVo> queryStoreTaskDistributeInfo(Long areaTaskId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("areaTaskId", areaTaskId);
        return dalClient.queryForList("areaTask.queryStoreTaskDistributeInfo", param, CalStoreTaskDistributeVo.class);
    }

}
